# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
from .user_setting import MYSQL_HOST,MYSQL_DBNAME,MYSQL_USER,MYSQL_PASSWD
class TaobaoMySqlPipeline(object):
    def __init__(self):
        self.connect = pymysql.connect(
        host =   MYSQL_HOST,
        db =     MYSQL_DBNAME,
        user =   MYSQL_USER,
        passwd = MYSQL_PASSWD,
        port=3306,
        charset = 'utf8',
        use_unicode = True
        )
        self.cursor = self.connect.cursor()
        
    def process_item(self, item, spider):
        if(item['f']=='shop'):
            sql = 'insert into shop(nid,user_id,name_pat,price_pat,sales_pat,nick_pat,city_pat,pic_url) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)'
            lis = (item['nid'],item['user_id'],item['name_pat'],item['price_pat'],item['sales_pat'],item['nick_pat'],item['city_pat'],item['pic_url'])
            self.cursor.execute(sql,lis)
            self.connect.commit()
        else:
            #sql = 'insert info review(nid,user_id,content,rateDate,auctionSku) VALUES (%s,%s,%s,%s,%s)'
            sql = "INSERT INTO `tb`.`review`(`rid`,`nid`, `user_id`, `content`, `rateDate`, `auctionSku`) VALUES (%s,%s,%s,%s,%s,%s)"
            try:
                lis = (item['id'],item['nid'],item['user_id'],item['rateContent'],item['rateDate'],item['auctionSku'])
                self.cursor.execute(sql,lis)
                self.connect.commit()
            except:
                print("重复数据")
           
        return item
        


